#! /bin/sh

# Copyright (c) 2006-2012 Google, Inc. and contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

if test $# = 0; then
  echo trigger file:evlan/public/evlan_test.ekam-rule
  exit 0
fi

echo findInput evlan/public/evlan
read EVLAN

if test "$EVLAN" == ""; then
  echo "error:  couldn't find evlan executable." >&2
  exit 1
fi

echo "Computing 123 + 654 with debug memory manager and no support code..." >&2

echo newOutput "evlan/public/evlan_test_tmp/src.evlan"
read SRC
cat > $SRC << EOF
import "builtin".integer.add(123, 654)
EOF

echo newOutput "evlan/public/evlan_test_tmp/output"
read OUT
$EVLAN --debug_memory_manager < $SRC > $OUT
RESULT=`cat $OUT`

if [ "$RESULT" != 777 ]; then
  echo "FAILED" >&2
  echo "Expected: 777" >&2
  echo "Actual: $RESULT" >&2
  exit 1
fi

echo "Computing fib(15) with brainded memory manager and full support code..." >&2

cat > $SRC << EOF
fib(15) where
  fib = x => if x < 2 then x else fib(x - 1) + fib(x - 2)
EOF

$EVLAN < $SRC > $OUT
RESULT=`cat $OUT`

if [ "$RESULT" != 610 ]; then
  echo "FAILED" >&2
  echo "Expected: 610" >&2
  echo "Actual: $RESULT" >&2
  exit 1
fi

echo passed
exit 0
